<?php

namespace App\Service\OrderService\OrderRouteService;
use App\Model\PssModel\OrderSupplier;
use App\Service\Code\Normal;
use App\Service\OrderService\BaseSupplierRouterService;
use App\Service\TraitService\SupplierRouterOrderTrait;
use App\Service\VendorClient\Shopblackant\ShopblankantApiClient;

class Shopblackant extends BaseSupplierRouterService
{
    use SupplierRouterOrderTrait;

    /**
     * 订单申报
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        return $this->callSdkApi(ShopblankantApiClient::class,$param['apiBody'])->createOrder($param);
    }

    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }

    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale($param = [])
    {
        return $this->callSdkApi(ShopblankantApiClient::class,$param['apiBody'])->cancelOrder($param);
    }

    /**
     * 获取物流信息
     * @param array $param
     */
    public function orderExpress( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }

    /**
     * 商品列表查询接口
     * @param array $param
     * @return mixed
     */
    public function queryGoodsList( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }

    /**
     * 获取物流信息
     * @param array $param
     */
    public function queryOrderExpress( $param = [] )
    {
        $queryBody = $param['queryBody'];
        $apiBody   = $param['apiBody'];
        $orderID   = $queryBody['orderID'];
        $orderInfo = OrderSupplier::instance()->info(['id'=>$orderID,'company_id'=>$queryBody['supplierID']]);
        if( empty($orderInfo) ) {
            return Normal::returnCode(Normal::FAIL,"订单信息不存在，请核对！");
        }

        $res = $this->callSdkApi(ShopblankantApiClient::class,$apiBody)->{__FUNCTION__}($orderInfo);
        if( $res['code'] != Normal::SUC ) {
            return Normal::returnCode(Normal::FAIL,$res['msg']);
        }

        $logistics  = $res['data']['data'];
        $statusName = $logistics['orderInfoStatusDesc']??'申报失败';
        //订单状态 0待处理 1待申报 2申报中 3申报成功 4申报失败 5待发货 6出库成功 7已取消
        if( in_array($logistics['orderInfoStatus'],[50,80]) ) {
            //这里要修改状态
            OrderSupplier::instance()
                ->where(['id'=>$orderID,'company_id'=>$queryBody['supplierID']])
                ->update([
                    'is_query_express' => 0,
                    'order_decalre_status'=> 2,//申报失败
                    'order_decalre_result'=> $statusName
                ]);
            return Normal::returnCode(Normal::FAIL,$statusName);
        }
        //放行和出库
        if (in_array($logistics['orderInfoStatus'],[20,30,40])){
            return  Normal::returnCode(Normal::SUC,"已发货",[
                'expressInfo' => [
                    'express_name'  => $logistics['logisticsName'],
                    'express_sn'    => $logistics['expressNo'],
                ]
            ]);
        }
        return Normal::returnCode(Normal::FAIL,'还未发货');
    }


}